package com.wutongyu.mannyburger;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;

public class VideoPlayActivity extends AppCompatActivity {
    private static final String TAG = "VideoPlayActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);

        // 绑定控件
        VideoView videoView = findViewById(R.id.videoView);
        String videoPath = getIntent().getStringExtra("videoPath");

        // 检查视频路径是否有效
        if (videoPath == null || videoPath.isEmpty()) {
            Log.e(TAG, "无效的视频路径");
            Toast.makeText(this, "无效的视频路径", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        try {
            // 检查视频路径是否为 raw 文件夹中的资源,根据于不同情况设置不同的播放路径
            if (videoPath.startsWith("android.resource://")) {
                videoView.setVideoURI(Uri.parse(videoPath));
            } else {
                videoView.setVideoURI(Uri.parse("file://" + videoPath));
            }
            // 调用视频播放方法
            videoView.start();
        } catch (Exception e) {
            Log.e(TAG, "视频播放失败: " + e.getMessage(), e);
            Toast.makeText(this, "视频播放失败", Toast.LENGTH_SHORT).show();
            finish();
        }
    }
}
